package com.example.classwork;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MyListActivity extends ListActivity implements Runnable {
    private static final String TAG = "list";
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        List<String> list1 = new ArrayList<String>();
        for (int i = 1; i < 100; i++) {
            list1.add("item" + i);
        }
        String[] list_data = {"one", "tow", "three", "four"};
        ListAdapter adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, list_data);
        setListAdapter(adapter);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {

                if (msg.what == 5) {
                    List<String> list2 = (List<String>) msg.obj;
                    ListAdapter adapter = new ArrayAdapter<String>(MyListActivity.this, android.R.layout.simple_list_item_1, list2);
                    setListAdapter(adapter);
                }
                super.handleMessage(msg);
            }
        };

      //Thread t = new Thread(this);

     // t.start();
        MyTask myTask=new MyTask();
        myTask.setHandler(handler);
        Thread t=new Thread(myTask);
        t.start();
    }


   @Override
    public void run() {

        Log.i(TAG, "run:Running......");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        List<String> retList = new ArrayList<String>();
        try {
            Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
            String title = doc.title();
            Log.i(TAG, "run:title=" + title);
            Elements tables = doc.getElementsByTag("table");
            Element table = tables.get(1);

            Elements trs = table.getElementsByTag("tr");
            for (Element tr : trs) {
                Elements tds = tr.getElementsByTag("td");
                Log.i(TAG, "run:td.size=" + tds.size());

                if (tds.size() > 0) {
                    String td1 = tds.get(0).text();
                    String td2 = tds.get(5).text();
                    Log.i(TAG, "run:" + td1 + "==>" + td2);
                    retList.add(td1 + "==>" + td2);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Message msg = handler.obtainMessage(5,retList);
        //msg.obj = "Hello";

        handler.sendMessage(msg);
        Log.i(TAG, "run:sendMessage ok");
    }
}